import { defineStore } from 'pinia'
import {
  deletePageData,
  getPageListData,
  createPageData,
  editPageData
} from '@/service/main/system'

interface IUserData {
  usersList: any[]
  usersCount: number
  roleList: any[]
  roleCount: number
  goodsList: any[]
  goodsCount: number
  menuList: any[]
  menuCount: number
  departmentList: any[]
  departmentCount: number
}

export const useSystemStore = defineStore('system', {
  state: (): IUserData => ({
    usersList: [], // 两个数据是有关联的
    usersCount: 0,
    roleList: [],
    roleCount: 0,
    goodsList: [],
    goodsCount: 0,
    menuList: [],
    menuCount: 0,
    departmentList: [],
    departmentCount: 0
  }),

  actions: {
    // 判断函数 根据传入的页面名字，来拼接 然后存储对应的数据
    judgeThePageName(stateList: string, list: any, totalCount: any) {
      // 这里的名字都是小写的 比如menulist 千万别不要再写成menuList了 这样获取不到数据的
      if (stateList === 'userslist') {
        this.usersList = list
        this.usersCount = totalCount
      }
      if (stateList === 'rolelist') {
        this.roleList = list
        this.roleCount = totalCount
      }
      if (stateList === 'goodslist') {
        this.goodsList = list
        this.goodsCount = totalCount
      }
      if (stateList === 'menulist') {
        this.menuList = list
        this.menuCount = totalCount
      }
      if (stateList === 'departmentlist') {
        this.departmentList = list
        this.departmentCount = totalCount
      }
    },

    // 请求页面数据
    async getPageListAction(pageName: string, queryInfo: any) {
      const pageUrl = `/${pageName}/list`
      // 拼接 state 数组的名称 例如 usersList，然后根据对应的保存数据 其实也可以直接用pageUrl的
      const stateList = `${pageName}list`
      // 获取用户列表
      const getPageListResult = await getPageListData(pageUrl, queryInfo)
      // 把数据存储到state里面
      const { list, totalCount } = getPageListResult.data
      // 调用判断函数 存储不同数据
      this.judgeThePageName(stateList, list, totalCount)
    },

    // 删除操作
    async delPageDataAction(data: { pageName: string; id: number }) {
      // 1.获取pageName和id
      const { pageName, id } = data
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl) // 2.调用删除的网络请求
      // 3. 重新获取最新页面数据
      this.getPageListAction(pageName, { offset: 0, size: 10 })
    },

    // 创建数据
    async createPageDataAction(data: { pageName: string; newData: any }) {
      // 1、创建数据请求
      const { pageName, newData } = data
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      // 2、重新获取最新页面数据
      this.getPageListAction(pageName, { offset: 0, size: 10 })
    },

    // 编辑数据
    async editPageDataAction(data: {
      pageName: string
      id: number
      editData: any
    }) {
      // 1、编辑的数据请求
      const { pageName, editData, id } = data
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)
      // 2、重新获取最新页面数据
      this.getPageListAction(pageName, { offset: 0, size: 10 })
    }
  },

  getters: {
    // 根据不同页面返回不同数据
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`] ?? []
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`] ?? 0
      }
    }
  }
})
